<template>
	<section>
		<!--工具条-->
		<el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
			<el-form :model="filters" :inline="true">
				<el-form-item>
					<el-input v-model="filters.keywords" placeholder="关键字" ></el-input>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" v-on:click="getTenants">查询</el-button>
				</el-form-item>
				<el-form-item>
					<el-button type="primary" >新增</el-button>
				</el-form-item>
			</el-form>
		</el-col>

		<!--列表v-loading="listLoading"-->
		<el-table :data="tenants" v-loading="listLoading" highlight-current-row  style="width: 100%;">
			<!--多选框-->
			<el-table-column type="selection" width="55">
			</el-table-column>
			<!--索引值,为什么不用id,id不序号-->
			<el-table-column type="index" width="60">
			</el-table-column>
			<!--其他都设置值,只有一个不设置值就自动适应了-->
			<el-table-column prop="companyName" label="机构名称">
			</el-table-column>
			<el-table-column prop="companyNum" label="营业执照">
			</el-table-column>
			<el-table-column prop="address" label="公司地址">
			</el-table-column>
			<el-table-column prop="logo" label="LOGO">
			</el-table-column>


			<!--'0 待审核，1 审核通过待激活 ，2.激活成功  3.审核失败 4.激活超时'-->
			<el-table-column prop="state" label="状态" sortable>
				<template scope="scope">
					<span size="small" v-if="scope.row.state === 0" style="color: gray">待审核</span>
					<span size="small" v-else-if="scope.row.state === 1" style="color: green">审核通过待激活</span>
					<span size="small" v-else-if="scope.row.state === 2" style="color: greenyellow">激活成功</span>
					<span size="small" v-else-if="scope.row.state === 3" style="color: red">审核失败</span>
					<span size="small" v-else-if="scope.row.state === 4" style="color: cornflowerblue">激活超时</span>
				</template>
			</el-table-column>


			<el-table-column label="操作" >
				<template scope="scope">
					<el-button  size="small"  @click="openAudit(scope.row)">审核</el-button>
				</template>
			</el-table-column>
		</el-table>
		<!--工具条-->
		<el-col :span="24" class="toolbar">
			<el-button type="danger">批量删除</el-button>
			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"  :page-size="10" :total="total" style="float:right;">
			</el-pagination>
		</el-col>

		<!--审核对话框-->
		<el-dialog title="机构审核"  :visible.sync="formVisible" :close-on-click-modal="false">
			<el-form :model="tenant" label-width="80px" :rules="formRules" ref="tenant">
				<el-form-item label="机构名" prop="name">
					<el-input v-model="tenant.companyName" auto-complete="off"></el-input>
				</el-form-item>
				<el-input
						type="textarea"
						:rows="4"
						placeholder="审核备注"
						v-model="textarea">
				</el-input>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button @click.native="formVisible = false" type="warning">审核驳回</el-button>
				<el-button type="primary" @click="audit" >审核通过</el-button>
			</div>
		</el-dialog>
	</section>
</template>

<script>
    export default {
        data() {
            return {

                textarea:"",
                formVisible:false,
                listLoading:false,
                //查询对象
                filters:{
                    keywords:''
                },
                page:1,//当前页,要传递到后台的
                total:0, //分页总数
                tenants:[], //当前页数据
                tenant:{

                },

                auditMessage:{
                    id:"",
                    auditRemark:""
                }
            }
        },
        methods: {
            //编辑模态框


            openAudit(row){
                //回显
                let tenantTmp = Object.assign({}, row); //解决对话框改值后列表会被改值.

                this.tenant = tenantTmp; //里面本来就有id,相当于回显了id
                console.log(this.tenant)



                //显示
                this.formVisible =true;
                /*
                        审核信息
                        * 查询第三方工商认证接口，核实企业材料，
                        * 并把结果显示到弹窗中，如：“企业材料，信用编码无误”  或  “企业材料不准确”
                        * */
                this.$http.post("/system/tenant/verify",this.tenant).then((res)=>{
                    this.auditMessage.auditRemark = this.textarea;
                    let result = res.data;
                    console.log(result);

                    if(result.success){
                        this.$message({
                            message: result.message,
                            type: 'success'
                        });
                    }else {
                        this.$message({
                            message: result.message,
                            type: 'error'
                        });
                    }
                })

            },

            audit(){
                this.$refs.tenant.validate((valid) => {
                    //校验表单成功后才做一下操作
                    if (valid) {
                        this.$confirm('确认提交吗？', '提示', {}).then(() => {
                            //拷贝后面对象的值到新对象,防止后面代码改动引起模型变化
                            let para = Object.assign({}, this.tenant);
                            this.auditMessage.id=para.id;
                            this.auditMessage.auditRemark=this.textarea;
                            //auditMessage:公司id  备注信息  通过公司id查询adminId  再根据adminId查询emp管理员  得到邮箱
                            //点击审核通过修改状态为1  审核通过待激活
                            //将备注信息放到邮件中，邮件中附带链接，点击链接，修改公司状态为
                            //审核通过，修改状态为审核通过待激活 发送邮件让用户确认，用户确认 激活成功 修改状态为2
                            this.$http.post("/system/tenant/confirmByEamil",this.auditMessage).then((res) => {
                                this.$message({
                                    message: '操作成功!',
                                    type: 'success'
                                });
                                //重置表单
                                this.$refs['tenant'].resetFields();
                                //关闭对话框
                                this.formVisible = false;
                                //刷新数据
                                this.getTenants();
                            });
                        });
                    }
                })
            },
            handleCurrentChange(curentPage){
                this.page = curentPage;
                this.getTenants();
            },
            getTenants(){
                //发送Ajax请求后台获取数据  axios
                //添加分页条件及高级查询条件
                let para = {
                    "page":this.page,
                    "keyword":this.filters.keywords
                };
                this.listLoading = true; //显示加载圈
                //分页查询
                this.$http.post("/system/tenant/pagelist",para) //$.Post(.....)
                    .then(result=>{
                        this.total = result.data.total;
                        this.tenants = result.data.rows;
                        this.listLoading = false;  //关闭加载圈
                    });
                /* 查询所有
                this.$http.patch("/department",para) //$.Post(.....)
                    .then(result=>{
                        console.log(result);
                        console.log(result.data);
                        this.tenants = result.data;
                        this.listLoading = false;  //关闭加载圈
                    });
                    */
            }
        },
        mounted() {
            this.getTenants()
        }
    }

</script>

<style scoped>

</style>